/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sea.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 *
 * The class of file operation
 * 
 * @author  Zhirun Ding
 * 
 * Created on 06/07/2012
 * 
 */

public class FileHelper {
    public static BufferedReader bufread;

    /**
     * create txt file
     * @throws IOException 
     * 
     */
    public static boolean createTxtFile(String path) throws IOException{
        File filename = new File(path);
        if (!filename.exists()) {
            filename.createNewFile();

            System.err.println(filename + " has been created！");
            return true;
        } else {
            return false;
        }
    }        

    /**
     * read data from text file
     * 
     */
    public static String readTxtFile(String path){
        File filename = new File(path);
        String read;
        FileReader fileread;
        String readStr = "";
        try {
            fileread = new FileReader(filename);
            bufread = new BufferedReader(fileread);
            try {
                while ((read = bufread.readLine()) != null) {
                    readStr = readStr + read + "\r\n";
                }
                bufread.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //System.out.println("content of file:"+ "\r\n" + readStr);
        return readStr;
    }

    /**
     * write data to txt file
     * 
     */
    public static void writeTxtFile(String path, String newStr) throws IOException{
        File file = new File(path);   
        if(!file.exists())   
            file.createNewFile();   
        FileOutputStream out = new FileOutputStream(file, false);           
        out.write(newStr.getBytes("utf-8"));          
        out.close();   
    }

    /**
     * replace data
     * 
     * @param oldStr
     *            
     * @param replaceStr
     *           
     */
    public static void replaceTxtByStr(String path, String oldStr, String replaceStr) {
        String temp = "";
        try {
            File file = new File(path);
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            StringBuffer buf = new StringBuffer();

            //save the content before the line
            for (int j = 1; (temp = br.readLine()) != null
                    && !temp.equals(oldStr); j++) {
                buf = buf.append(temp);
                buf = buf.append(System.getProperty("line.separator"));
            }

            // insert it
            buf = buf.append(replaceStr);

            // save
            while ((temp = br.readLine()) != null) {
                buf = buf.append(System.getProperty("line.separator"));
                buf = buf.append(temp);
            }

            br.close();
            FileOutputStream fos = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(fos);
            pw.write(buf.toString().toCharArray());
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

        
    /**
     * copy file
     * @param oldPath String source file 如：c:/fqf.txt
     * @param newPath String des file 如：f:/fqf.txt
     */
    public static void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) {
                //when the file exist
                InputStream inStream = new FileInputStream(oldPath); 
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; 
                    //System.out.println(bytesum);
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
            System.out.println("success to copy single file!");
        } catch (Exception e) {
            System.out.println("failure  to copy single file!");
            e.printStackTrace();
        }
    }

    /**
     * delete file
     * @param path
     */
    public static void deleteFile(String filePath) {
        try {
            File myDelFile = new File(filePath);
            if (myDelFile.exists()) {
                myDelFile.delete();
            }
            System.out.println("success to delete file!");
        } catch (Exception e) {
            System.out.println("failure to delete file!");
            e.printStackTrace();
        }
    }        
}
